package com.lbkj.ops.common.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author jian.liu
 * @date 2020/10/28
 */
public class ServletUtils {

    public final static String HEADER_HTTP_X_REAL_IP = "X-REAL-IP";
    public final static String HEADER_X_FORWARDED_FOR = "X-Forwarded-For";
    public final static String HEADER_PROXY_CLIENT_IP = "Proxy-Client-IP";
    public final static String HEADER_WL_PROXY_CLIENT_IP = "WL-Proxy-Client-IP";
    public final static String HEADER_HTTP_CLIENT_IP = "HTTP_CLIENT_IP";
    public final static String HEADER_HTTP_X_FORWARDED_FOR = "HTTP_X_FORWARDED_FOR";
    public final static String UNKNOWN = "unkonwn";

    private static ServletRequestAttributes getServletRequestAttributes(){
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = getServletRequestAttributes();
        if(requestAttributes == null) return null;
        return requestAttributes.getRequest();
    }

    public static HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = getServletRequestAttributes();
        if(requestAttributes == null) return null;
        return requestAttributes.getResponse();
    }

    public static HttpSession getSession(){
        HttpServletRequest request = getRequest();
        if(request == null) return null;
        return request.getSession();
    }

    /**
     * 获取网站地址
     * @return
     */
    public static String getWebPath() {
        HttpServletRequest request = getRequest();
        if(request == null) return null;
        return getRequest().getScheme()+"://"+getRequest().getServerName()+":"+getRequest().getServerPort();
    }

    /**
     * 获取当前请求跟路径
     * @return
     */
    public static String getBasePath() {
        HttpServletRequest request = getRequest();
        if(request == null) return null;
        String path = getRequest().getContextPath();
        if(!path.endsWith("/")){
            path += "/";
        }
        String basePath = getRequest().getScheme()+"://"+getRequest().getServerName()+":"+getRequest().getServerPort()+path;
        return basePath;
    }

    public static String getRealIp(){
        HttpServletRequest request = getRequest();
        if(request == null) return null;
        return request.getHeader(HEADER_HTTP_X_REAL_IP);
    }

    /**
     * 获取IP地址
     * 注意：X-Forwarded-For不可靠， 可以由客户端伪造
     * @return
     */
    public static String getIpAddress(){
        HttpServletRequest request = getRequest();
        if(request == null) return null;
        String ip = request.getHeader(HEADER_X_FORWARDED_FOR);
        if(StringUtils.isEmpty(ip) || ip.equalsIgnoreCase(UNKNOWN)){
            ip = request.getHeader(HEADER_PROXY_CLIENT_IP);
            if(!StringUtils.isEmpty(ip) && !ip.equalsIgnoreCase(UNKNOWN)){
                return ip;
            }
            ip = getRequest().getHeader(HEADER_WL_PROXY_CLIENT_IP);
            if(!StringUtils.isEmpty(ip) && !ip.equalsIgnoreCase(UNKNOWN)){
                return ip;
            }
            ip = getRequest().getHeader(HEADER_HTTP_CLIENT_IP);
            if(!StringUtils.isEmpty(ip) && !ip.equalsIgnoreCase(UNKNOWN)){
                return ip;
            }
            ip = getRequest().getHeader(HEADER_HTTP_X_FORWARDED_FOR);
            if(!StringUtils.isEmpty(ip) && !ip.equalsIgnoreCase(UNKNOWN)){
                return ip;
            }
            ip = getRequest().getRemoteAddr();
            return ip;
        } else if(ip.length() > 15){
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }
}
